package com.siwei.mes.sync.tc;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.entity.sync.tc.SupplyTask;
import com.siwei.mes.enums.SyncEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 对外给砼材-供货任务相关
 *
 * @author linzi
 * @since 2024-05-22 21:08:27
 */
@Api(value = "对外给砼材-供货任务")
@RestController
@RequestMapping("tc/supplyTask")
public class SyncTcSupplyTaskController {
    private static final Logger log = LoggerFactory.getLogger("tc");

    @Value("${tc-token}")
    private String TC_TOKEN;

    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(TC_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "新增或修改供货任务表")
    @PostMapping("/saveOrUpdate")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) SupplyTask entity, HttpServletRequest request) {
        log.info("砼材新增或修改供货任务,入参：{}", JSON.toJSONString(entity));
        this.checkToken(request);
        entity.setSyncType(SyncEnum.TC.getCode());
        return Json.succ().data(new ApiResult());
    }

}
